package cn.weblog.api.controller;

import cn.weblog.api.domain.User;
import cn.weblog.api.service.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/findOne/{userId}")//t
	public String findOne(@PathVariable Integer userId){
		User user = userService.findByUserId(userId);
		Map<String,Object> result=new HashMap<>();
		result.put("user",user);
		return JSON.toJSONString(result);
	}

	@PostMapping("/update")//t 更新用户信息  记得传入userID
	public String updateUser(@RequestBody User user){
		int update = userService.update(user);
		Map<String,Object> result=new HashMap<>();
		if (update==1){
			//会直接覆盖原来的值吗？会。
			user=userService.findByUserId(user.getUserId());
			result.put("user",user);
			//model.addAttribute("updateMsg","更新成功");
			return JSON.toJSONString(result);
		}
		//更新失败
		result.put("Msg","更新个人信息失败");
		return JSON.toJSONString(result);
	}

}
